//
//  KDPullDownMenu.m
//  PublicSentimentMonitorSystem
//
//  Created by 魏强 on 15/7/1.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "KDPullDownMenu.h"
#import "KDPullDownMenuModel.h"
#define TypeLabel_H 25
#define TitleView_H 30
#define CellH 40
#define TAP 30 //距离左右的边距
#define noDisableVerticalScrollTag 836913
#define noDisableHorizontalScrollTag 836914
@interface KDPullDownMenu ()<UITableViewDataSource, UITableViewDelegate>
/**
 *  总类型 eg：网站类型
 */
@property (nonatomic, strong) UILabel *typeLabel;
/**
 *  显示的那个View
 */
@property (nonatomic, strong) UIButton *titleView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, weak) UIImageView *iv;

@end
@implementation KDPullDownMenu

+ (instancetype)pullDownMenuWithFrame:(CGRect)frame
{
    return [[self alloc]initWithFrame:frame];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = BackGroundColor;
        [self setUp];
    }
    return self;
}
- (void)drawRect:(CGRect)rect {
}
- (void)setModel:(KDPullDownMenuModel *)model
{
    _model = model;
//    _model.index = [model.allIdArr[0] intValue];//刚开始的时候，默认第一个
    self.typeLabel.text = model.typeName;
    if (model.required == YES) {//如果是必填的，就加标注
        self.iv.width = self.iv.image.size.width;
        self.typeLabel.x = CGRectGetMaxX(self.iv.frame) +8;
    }
//    KDLog(@"%@", NSStringFromCGRect(self.iv.frame));
    CGRect labelRect = [model.allTypeArr[model.index] boundingRectWithSize:CGSizeMake(screenW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
//    NSString *title = (self.model.titleName)?self.model.titleName:model.allTypeArr[0];
    [self.titleView setTitle:model.allTypeArr[model.index] forState:UIControlStateNormal];
    _titleView.titleEdgeInsets = UIEdgeInsetsMake(0, -_titleView.width+labelRect.size.width, 0, 0);
    [self.tableView reloadData];
    [self setNeedsDisplay];
}
/**
 *  设置版面
 */
- (void)setUp
{
//    创建类型标题label
    UIImage *image = [UIImage imageNamed:@"标注"];
    UIImageView *iv = [[UIImageView alloc]initWithImage:image];
    iv.frame = CGRectMake(0, (TypeLabel_H - image.size.height) * 0.5, 0, image.size.height);
    self.iv = iv;
//    KDLog(@"%@", NSStringFromCGRect(iv.frame));
    UILabel *typeLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.width, TypeLabel_H)];
    self.typeLabel = typeLabel;
    typeLabel.font = [UIFont boldSystemFontOfSize:15];
    typeLabel.textColor = WQColor(87, 87, 87);
    typeLabel.textAlignment = NSTextAlignmentLeft;
    
    //创建标题点击view
    UIButton *titleView = [UIButton buttonWithType:UIButtonTypeCustom];
    titleView.backgroundColor = [UIColor whiteColor];
    self.titleView = titleView;
    titleView.frame = CGRectMake(0, CGRectGetMaxY(typeLabel.frame), self.width, TitleView_H);
    [titleView setImage:[UIImage imageNamed:@"类型下"] forState:UIControlStateNormal];
    [titleView setImage:[UIImage imageNamed:@"类型上"] forState:UIControlStateSelected];
    titleView.imageEdgeInsets = UIEdgeInsetsMake(0, titleView.width-25, 0, 0);

    [titleView setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    titleView.titleLabel.font = [UIFont systemFontOfSize:15];
    [titleView addTarget:self action:@selector(titleViewClick:) forControlEvents:UIControlEventTouchUpInside];
    //创建下拉的tableView
    UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(titleView.frame), self.width, 0) style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.scrollEnabled = YES;
    tableView.showsVerticalScrollIndicator = YES;
    tableView.rowHeight = CellH;
    tableView.tag = noDisableVerticalScrollTag;
    [tableView flashScrollIndicators];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [self.tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    [self addSubview:iv];
    [self addSubview:typeLabel];
    [self addSubview:titleView];
    [self addSubview:tableView];
}

#pragma mark - 事件点击区
/**
 *  上下拉按钮被点击了
 *
 *  @param titleView
 */
- (void)titleViewClick:(UIButton *)titleView{
    if (_delegate && [_delegate respondsToSelector:@selector(KDPullDownMenu:PullDownOrPullUp:)]) {
        [_delegate KDPullDownMenu:self PullDownOrPullUp:!titleView.selected];
    }
    NSInteger num;
    if (self.model.allTypeArr.count < 5) {
        num = self.model.allTypeArr.count;
    }else{
        num = 5 ;
    }
    if (!titleView.selected) {//需要下拉
        [UIView animateWithDuration:0.2f animations:^{
            self.height = CGRectGetMaxY(self.titleView.frame)+CellH*num+10;
            self.tableView.height = CellH*num;
            [self setNeedsDisplay];
        }];
    }else{//需要上拉
        [UIView animateWithDuration:0.2f animations:^{
            self.tableView.height = 0;
            self.height = CGRectGetMaxY(self.titleView.frame)+ 10;
            [self setNeedsDisplay];
        }];
    }
    titleView.selected = !titleView.selected;
}
#pragma mark - UITableViewDataSource

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.model.allTypeArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId =@"reportCell";
    UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }
    cell.textLabel.text = self.model.allTypeArr[indexPath.row];
    cell.textLabel.textColor = [UIColor grayColor];
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.model.index = (int)indexPath.row;
//    KDLog(@"%@", self.model.index);
    
    [self.titleView setTitle:self.model.allTypeArr[indexPath.row] forState:UIControlStateNormal];
    CGRect labelRect = [self.model.allTypeArr[indexPath.row] boundingRectWithSize:CGSizeMake(screenW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];

    _titleView.titleEdgeInsets = UIEdgeInsetsMake(0, -_titleView.width+labelRect.size.width, 0, 0);
    [self titleViewClick:self.titleView];
    
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];}
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];}
}
@end
